/*
 * Date         Author      Note
 * 2014.11.28   舞者无罪           参数设置界面
 * 2015.04.29   wzwz        增加伺服驱动参数
 * 2015.06.24   wzwz        增加返回
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "lang.h"
#include "resource.h"
#include "func.h"
#include "com.h"
#include "param.h"
#include "mhbtn.h"
#include "mhlabel.h"
#include "sys.h"
#include "work.h"
#include "rock.h"
#include "dumu.h"
#include "yarn_change.h"
#include "password.h"
#include "info.h"
#include "rock_drive.h"
#include "mac_drive.h"
#include "mac_param.h"
#include "yarn_before.h"
#include "cam.h"

/*
 *
 */
static void CreateParamInterface(HWND hWnd)
{
    char esc[64] = {0};
    sprintf(esc, GetString(802), 12);
    const char *sc[12] = {
            GetString(165),
            GetString(166),
            GetString(167),
            GetString(168),
            GetString(169),
            GetString(170),
            GetString(636),
            GetString(637),
            GetString(819),
            GetString(872),
            GetString(896),
            esc,
    };
    ushort id[12] = {
            ID_BTN_SYS,
            ID_BTN_WORK,
            ID_ROCK,
            ID_DUMU,
            ID_YARN_CHANGE,
            ID_INIT_PARAM,
            ID_BTN_MAC,
            ID_ROCK_DRIVE,
            ID_MAC_PARAM,
            ID_YARN_BEFORE_SET,
            ID_CAM_SETTING,
            ID_ESC,
    };
    int i = 0;
    for(i = 0; i < 9; ++i)
    {
        CreateWindow (MH_BTN_NAME,
            sc[i],
            WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
            id[i],
            30, 60 + i * 30, 200, 30,
            hWnd, 0);
    }
        for(i = 9; i < 12; ++i)
    {
        CreateWindow (MH_BTN_NAME,
            sc[i],
            WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
            id[i],
            30 + 200, 60 + (i - 9) * 30, 200, 30,
            hWnd, 0);
    }
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    switch(nID)
    {
    case ID_ROCK:
        {
            HWND hDlg = CreatePasswordDialog(hWnd);
            if(PasswordDoModal(hDlg))
            {
                CreateRockWindow(hWnd);
            }
        }
        break;
    case ID_DUMU:
        {
            HWND hDlg = CreatePasswordDialog(hWnd);
            if(PasswordDoModal(hDlg))
            {
                CreateDumuWindow(hWnd);
            }
        }
        break;
    case ID_YARN_CHANGE:
        {
            if(STATE_STOP == GetRunState(g_knit_data))
            {
                CreateYarnChangeWindow(hWnd);
            }
            else
            {
                AfxInfoBox(hWnd, NULL, GetString(461));
            }
        }
        break;
    case ID_INIT_PARAM:
        {
            HWND hDlg = CreatePasswordDialog(hWnd);
            if(hDlg != HWND_INVALID)
            {
                if(PasswordDoModal(hDlg))
                {
                    TurnOnMachine(g_knit_data, FALSE);
                    char sc[MH_FILE_NAME_MAX_LEN] = {0};
                    strcat(sc, BACKUP_MAC_PARAM_FILE);
                    int ret = ReadMacFile(sc, g_knit_data);
                    if(ret)
                    {
                        AddSysLog(g_knit_data, LOG_TYPE_FILE, ret);
                        printf("Failed to read mac file: %d\n", ret);
                    }
                    else
                    {
                        sc[0] = 0;
                        strcat(sc, MAC_PARAM_FILE);
                        ret = WriteMacFile(sc, g_knit_data);
                        if(ret)
                        {
                            AddSysLog(g_knit_data, LOG_TYPE_FILE, ret);
                            printf("Failed to write mac file: %d\n", ret);
                        }
                        else
                        {
                            MH_FILE_SYNC();
                        }
                    }
                    TurnOnMachine(g_knit_data, TRUE);
                    sc[0] = 0;
                    if(ret)
                    {
                        strcat(sc, GetString(477));
                    }
                    else
                    {
                        SetWhichNeedDo(g_knit_data, NEED_DO_RESET_START);
                        strcat(sc, GetString(476));
                    }
                    AfxInfoBox(hWnd, NULL, sc);
                }
            }
        }
        break;
    case ID_BTN_SYS:
        if(STATE_STOP == GetRunState(g_knit_data))
        {
            HWND hDlg = CreatePasswordDialog(hWnd);
            if(PasswordDoModal(hDlg))
            {
                CreateSysWindow(hWnd);
            }
        }
    else
        {
            AfxInfoBox(hWnd, NULL, GetString(461));
        }
        break;
    case ID_BTN_WORK:
        {
            CreateWorkWindow(hWnd);
        }
        break;
    case ID_MAC_PARAM:
        if(STATE_STOP == GetRunState(g_knit_data))
        {
            HWND hDlg = CreatePasswordDialog(hWnd);
            if(PasswordDoModal(hDlg))
            {
                CreateMacParamWindow(hWnd);
            }
        }
        else
        {
            AfxInfoBox(hWnd, NULL, GetString(461));
        }
        break;
    case ID_BTN_MAC:
        if(STATE_STOP == GetRunState(g_knit_data))
        {
            HWND hDlg = CreatePasswordDialog(hWnd);
            if(PasswordDoModal(hDlg))
            {
                CreateMacDriveWindow(hWnd);
            }
        }
        else
        {
            AfxInfoBox(hWnd, NULL, GetString(461));
        }
        break;
    case ID_ROCK_DRIVE:
        if(STATE_STOP == GetRunState(g_knit_data))
        {
            HWND hDlg = CreatePasswordDialog(hWnd);
            if(PasswordDoModal(hDlg))
            {
                CreateRockDriveWindow(hWnd);
            }
        }
        else
        {
            AfxInfoBox(hWnd, NULL, GetString(461));
        }
        break;
    case ID_YARN_BEFORE_SET:
         if(STATE_STOP == GetRunState(g_knit_data))
        {
            HWND hDlg = CreatePasswordDialog(hWnd);
            if(PasswordDoModal(hDlg))
            {
                CreateYarnBeforeWindow(hWnd);
            }
        }
        else
        {
            AfxInfoBox(hWnd, NULL, GetString(461));
        }
        break;
    case ID_CAM_SETTING:
          if(STATE_STOP == GetRunState(g_knit_data))
        {
            HWND hDlg = CreatePasswordDialog(hWnd);
            if(PasswordDoModal(hDlg))
            {
                CreateCamWindow(hWnd);
            }
        }
        else
        {
            AfxInfoBox(hWnd, NULL, GetString(461));
        }
        break;
    case ID_ESC:
        {
            PostMessage (hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    }
}
/*
 *
 */
static void OnMoveFocus(HWND hWnd, int up)
{
    ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
    switch(nID)
    {
    case ID_BTN_SYS:
        if(up)
        {
            //
        }
        else
        {
            nID = ID_BTN_WORK;
        }
        break;
    case ID_BTN_WORK:
        if(up)
        {
            nID = ID_BTN_SYS;
        }
        else
        {
            nID = ID_ROCK;
        }
        break;
    case ID_ROCK:
        if(up)
        {
            nID = ID_BTN_WORK;
        }
        else
        {
            nID = ID_DUMU;
        }
        break;
    case ID_DUMU:
        if(up)
        {
            nID = ID_ROCK;
        }
        else
        {
            nID = ID_YARN_CHANGE;
        }
        break;
    case ID_YARN_CHANGE:
        if(up)
        {
            nID = ID_DUMU;
        }
        else
        {
            nID = ID_INIT_PARAM;
        }
        break;
    case ID_INIT_PARAM:
        if(up)
        {
            nID = ID_YARN_CHANGE;
        }
        else
        {
            nID = ID_BTN_MAC;
        }
        break;
    case ID_BTN_MAC:
        if(up)
        {
            nID = ID_INIT_PARAM;
        }
        else
        {
            nID = ID_ROCK_DRIVE;
        }
        break;
    case ID_ROCK_DRIVE:
        if(up)
        {
            nID = ID_BTN_MAC;
        }
        else
        {
            nID = ID_MAC_PARAM;
        }
        break;
    case ID_MAC_PARAM:
        if(up)
        {
            nID = ID_ROCK_DRIVE;
        }
        else
        {
            nID = ID_YARN_BEFORE_SET;
        }
        break;
    case ID_YARN_BEFORE_SET:
        if(up)
        {
            nID = ID_MAC_PARAM;
        }
        else
        {
            nID = ID_CAM_SETTING;
        }
        break;
    case ID_CAM_SETTING:
        if(up)
        {
            nID = ID_YARN_BEFORE_SET;
        }
        else
        {
            nID = ID_ESC;
        }
        break;
    case ID_ESC:
        if(up)
        {
            nID = ID_CAM_SETTING;
        }
        break;
    }
    SetFocusChild(GetDlgItem(hWnd, nID));
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE || key == SCANCODE_9)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_1)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_BTN_SYS));
        OnBtnCmd(hWnd, ID_BTN_SYS);
    }
    else if(key == SCANCODE_2)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_BTN_WORK));
        OnBtnCmd(hWnd, ID_BTN_WORK);
    }
    else if(key == SCANCODE_3)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_ROCK));
        OnBtnCmd(hWnd, ID_ROCK);
    }
    else if(key == SCANCODE_4)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_DUMU));
        OnBtnCmd(hWnd, ID_DUMU);
    }
    else if(key == SCANCODE_5)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_YARN_CHANGE));
        OnBtnCmd(hWnd, ID_YARN_CHANGE);
    }
    else if(key == SCANCODE_6)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_INIT_PARAM));
        OnBtnCmd(hWnd, ID_INIT_PARAM);
    }
     else if(key == SCANCODE_7)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_BTN_MAC));
        OnBtnCmd(hWnd, ID_BTN_MAC);
    }
    else if(key == SCANCODE_8)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_ROCK_DRIVE));
        OnBtnCmd(hWnd, ID_ROCK_DRIVE);
    }
    else if(key == SCANCODE_9)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_MAC_PARAM));
        OnBtnCmd(hWnd, ID_MAC_PARAM);
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        OnMoveFocus(hWnd, FALSE);
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        OnMoveFocus(hWnd, TRUE);
    }
    else if(key == SCANCODE_ENTER)
    {
        OnBtnCmd(hWnd, GetDlgCtrlID(GetFocusChild(hWnd)));
    }
}
/*
 *
 */
static int ParamWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateParamInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += MASTER_TITLE_LEFT;
            rect.top += MASTER_TITLE_TOP;
            rect.right = rect.left + MASTER_TITLE_WIDTH - 1;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        DestroyMainWindow(hWnd);
        MainWindowCleanup (hWnd);
        return 0;
    case MSG_KEYDOWN:
        OnKeyDown(hWnd, wParam);
        return 0;
    case MSG_COMMAND:
        OnBtnCmd(hWnd, LOWORD(wParam));
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls (hWnd);
        }
        break;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_MAST]);
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateParamWindow(HWND hHosting)
{
    HWND hWnd;
    MAINWINCREATE CreateInfo;

    int x = (CX_SCREEN - MASTER_WND_CX) / 2;
    int y = (CY_SCREEN - MASTER_WND_CY) / 2;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_PARAM_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = ParamWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + MASTER_WND_CX;
    CreateInfo.by = y + MASTER_WND_CY;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_BTN_SYS));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    else
    {
    }
    return hWnd;
}
